package com.songsong.myblog2.controller;

import com.alibaba.fastjson.JSON;
import com.songsong.myblog2.component.StringAndArray;
import com.songsong.myblog2.domain.Article;
import com.songsong.myblog2.domain.User;
import com.songsong.myblog2.service.ArticleService;
import com.songsong.myblog2.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

@Controller
@RequestMapping("/findArticle")
public class FindArticleController {
    /*注入articleService*/
    @Resource(name = "articleService")
    private ArticleService articleService;
    /*注入userService*/
    @Autowired
    private UserService userService;
    /*查询文章controller*/

    /**
     * 分页查询文章到主页
     */
    /*接收条件数据跳转到主页*/
    @RequestMapping("/toTag/{tagName}")
    public String toTag(@PathVariable("tagName") String tagName, Model model) {
        model.addAttribute("change", tagName);
        return "anon/main";
    }

    /**
     * 分页查询所有的数据
     *
     * @param rows
     * @param pageNum
     * @return
     */
    @RequestMapping("/pageArticle")
    @ResponseBody
    public JSONArray myArticles(@RequestParam("rows") String rows,
                                @RequestParam("pageNum") String pageNum) {
        JSONArray allArticles = articleService.findAllArticles(rows, pageNum);
        return allArticles;
    }

    /**
     * 根据标签分页查询文章到主页
     */
    @RequestMapping("/byTag/{tagName}")
    public @ResponseBody
    JSONArray tagArticle(@PathVariable("tagName") String tagName, @RequestParam("rows") String rows,
                         @RequestParam("pageNum") String pageNum) {
        JSONArray allArticles = articleService.findArticlesByTag(rows, pageNum, tagName);
        return allArticles;
    }

    /**
     * 根据id获取文章到文章页
     */
    @RequestMapping("/{id}")
    public String findArticleById(@PathVariable("id") long id, Model model) {
        Article article = articleService.findArticleById(id);
        /*判断当前登录用户是不是该博客的拥有者  或者是没有登录  将该博文的浏览量+1*/
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        //根据用户手机号查找到该用户
        User user = userService.findUser(username);
        if (username == null || user.getId() != article.getUserId()) {
            /*将hot+1*/
            articleService.updateArticleHot(article.getId());
        }
        model.addAttribute("article", article);
        //返回标签数组
        String[] tags = StringAndArray.stringToArray(article.getArticleTags());
        model.addAttribute("tags", tags);
        return "authc/info";
    }

    /**
     * 用户登录到主页 分页获取该用户的所有的文章
     *
     * @param rows
     * @param pageNum
     * @return
     */
    @RequestMapping("/users")
    @ResponseBody
    public JSONArray findArticlesByUser(@RequestParam("rows") String rows,
                                        @RequestParam("pageNum") String pageNum) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        JSONArray userArticles = articleService.findArticlesByUser(username, rows, pageNum);
        return userArticles;
    }

    /**
     * 获得文章管理
     *
     * @return
     */
    @GetMapping("/getArticleManagement")
    @ResponseBody
    public JSONObject getArticleManagement(@RequestParam("rows") String rows,
                                           @RequestParam("pageNum") String pageNum) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        JSONObject returnJson = new JSONObject();
        if (username == null) {
            returnJson.put("status", 403);
            return returnJson;
        }
        return articleService.getArticleManagement(username, Integer.parseInt(rows), Integer.parseInt(pageNum));
    }

    /**
     * 获得文章管理
     *
     * @return
     */
    @GetMapping("/getArticleManagementAdmin")
    @ResponseBody
    public JSONObject getArticleManagementAdmin(@RequestParam("rows") String rows,
                                                @RequestParam("pageNum") String pageNum) {

        return articleService.getArticleManagementAdmin(Integer.parseInt(rows), Integer.parseInt(pageNum));
    }

    @RequestMapping("/bySearch")
    @ResponseBody
    public JSONArray findArticlesBySearch(@RequestParam("rows") String rows,
                                          @RequestParam("pageNum") String pageNum,
                                          @RequestParam("keyboard") String keyboard) {
        JSONArray userArticles = articleService.findArticlesBySearch(keyboard, rows, pageNum);
        return userArticles;
    }
}